#include "Model/Comment.hpp"
#include <boost/test/auto_unit_test.hpp>
#include <boost/thread.hpp>
#include <boost/thread/xtime.hpp>

namespace
{

using namespace Model;

// ---------------------------------------------------------------------------------------------------------------------

BOOST_AUTO_TEST_CASE( testCreation)
{
	boost::posix_time::ptime beginTime( boost::date_time::second_clock< boost::posix_time::ptime>::local_time());
	static const String AUTHOR_NAME = "Author";
	Comment comment( AUTHOR_NAME);
	BOOST_CHECK_EQUAL( AUTHOR_NAME, comment.authorName());
	BOOST_CHECK( comment.body().empty());
	BOOST_CHECK( beginTime <= comment.creationTime() );
	BOOST_CHECK( comment.creationTime() == comment.lastChangeTime());
}

// ---------------------------------------------------------------------------------------------------------------------

BOOST_AUTO_TEST_CASE( testCopyConstructor)
{
	static const String AUTHOR_NAME = "Author";
	Comment comment( AUTHOR_NAME);

	boost::posix_time::ptime beginTime( boost::date_time::second_clock< boost::posix_time::ptime>::local_time());

	Comment copy( comment);

	BOOST_CHECK_EQUAL( AUTHOR_NAME, copy.authorName());
	BOOST_CHECK( copy.body().empty());
	BOOST_CHECK( beginTime <= copy.creationTime() );
	BOOST_CHECK( copy.creationTime() == copy.lastChangeTime());
}

// ---------------------------------------------------------------------------------------------------------------------

BOOST_AUTO_TEST_CASE( testAssignment)
{
	static const String AUTHOR_NAME = "Author";

	boost::posix_time::ptime beginTime( boost::date_time::second_clock< boost::posix_time::ptime>::local_time());
	Comment comment( AUTHOR_NAME);

	Comment copy( "Other author");

	// Sleep 1 second, so the difference between last change and the rest is relevant.
	boost::xtime xt; 
	boost::xtime_get(&xt, boost::TIME_UTC); 
	xt.sec += 1; // change xt to next second 
	boost::thread::sleep(xt);

	boost::posix_time::ptime copyTime( boost::date_time::second_clock< boost::posix_time::ptime>::local_time());
	copy = comment;
	boost::posix_time::ptime afterCopyTime( boost::date_time::second_clock< boost::posix_time::ptime>::local_time());

	BOOST_CHECK_EQUAL( AUTHOR_NAME, copy.authorName());
	BOOST_CHECK( copy.body().empty());
	BOOST_CHECK( beginTime <= copy.creationTime() );
	BOOST_CHECK( copy.creationTime() == comment.creationTime());
	BOOST_CHECK( copy.lastChangeTime() >= comment.lastChangeTime());
	BOOST_CHECK( afterCopyTime >= copy.lastChangeTime());
}


}